<?php
namespace Admin\Controller;
use Think\Controller;

class BaseController extends Controller {
    protected $fld_lang;

    function __construct(){
        parent::__construct();
        $this->fld_lang = \Common\Common\WebAction::getLangField();

        $this->setToolbar();
    }

	public function setLangtable($tblName)
	{
        $fld = '`fld_name`,IFNULL(`' . $this->fld_lang . '`,`fld_name`) as `fld_value`';
        $module =M('SysLangtable');
        $lang = $module->field($fld)->where("tbl_name='".$tblName."'")->select();

        foreach ($lang as $value) {
            $this->assign($value[fld_name],$value[fld_value]);
        }
	}

    protected function setToolbar()
    {
        $mid = $_GET[_mid];
        $sql = "select icon_cls,btn_width,icon_align,handler,IFNULL($this->fld_lang,btn_name) as btn_name".
            " from vsys_pagebutton where moduleid='$mid' order by sort";
        $module = M();
        $data = $module->query($sql);
        $html = '';
        foreach ($data as $value) {
            $html .= "<a href=\"#\" class=\"easyui-linkbutton\"".
                "data-options=\"iconCls:'$value[icon_cls]',iconAlign:'$value[icon_align]',plain:true,width:$value[btn_width]\"".
                " onclick=\"$value[handler]\">$value[btn_name]</a>";
        }
        $this->toolbar = $html;
    }

    public function updateRows($module, $key)
    {
        if(isset($_POST[inserted])){
            $inserted = json_decode($_POST[inserted],true);
            $this->addRows($module, $inserted);
        }
        if(isset($_POST[updated])){
            $updated = json_decode($_POST[updated],true);
            $this->editRows($module, $updated);
        }
        if(isset($_POST[deleted])){
            $deleted = json_decode($_POST[deleted],true);
            $this->delRows($module, $deleted, $key);
        }
                
        $result['statuscode'] = 0;
        $result['statusmessage'] = L('SAVE_TIP');
        $this->ajaxReturn($result, 'json');
    }

    protected function addRows($module,$data)
    {
        foreach ($data as $value) {
            while (list($key, $val) = each($value)) {
                $inserted[$key] = $val;
            }
            $module->add($inserted);
        }
    }

    protected function editRows($module,$data)
    {
        foreach ($data as $value) {
            while (list($key, $val) = each($value)) {
                $updated[$key] = $val;
            }
            $module->setField($updated);
        }
    }

    protected function delRows($module,$data,$key)
    {
        foreach ($data as $value) {
            $module->where("id=$key")->delete();
        }
    }

    protected function handlerQueryparameters($parameters)
    {
        $strWhere = "";
        $arr_param = json_decode($parameters, true);
        foreach ($arr_param as $value) {
            if($value[brackets_l] && $value[brackets_l]!=''){
                $strWhere.= " $value[brackets_l]";
            }
            $strWhere.= " $value[fld_name]";
            $comparison = $value[comparison];
            switch ($comparison) {
                case '%like':
                    $strWhere.= " like '%$value[value]'";
                    break;
                case 'like%':
                    $strWhere.= " like '$value[value]%'";
                    break;
                case 'like':
                    $strWhere.= " like '%$value[value]%'";
                    break;
                
                default:
                    $strWhere.= " $comparison '$value[value]'";
                    break;
            }
            if($value[brackets_r] && $value[brackets_r]!=''){
                $strWhere.= " $value[brackets_r]";
            }
            if($value[logic] && $value[logic]!=''){
                $strWhere.= " $value[logic]";
            }
        }
        return $strWhere;
    }

    public function query($mid)
    {
        $this->parenthesis = L('QUERY_PARENTHESIS');
        $this->field = L('QUERY_FIELD');
        $this->comparison = L('QUERY_COMPARISON');
        $this->value = L('QUERY_VALUE');
        $this->operator = L('QUERY_OPERATOR');

        $this->display();
    }

    /**
     * 获取查询时需要用到的条件操作符，
     *
     * @return json
     * @author 
     **/
    public function getOperator()
    {
        $operators = array(
            array('key' => '=', 'val' => '='),
            array('key' => '!=', 'val' => '!='),
            array('key' => '>=', 'val' => '>='),
            array('key' => '<=', 'val' => '<='),
            array('key' => '%like', 'val' => '%like'),
            array('key' => 'like%', 'val' => 'like%'),
            array('key' => 'like', 'val' => 'like')
        );
        $this->ajaxReturn($operators, 'json');
    }

    /**
     * 获取页面的查询配置
     *
     * @return void
     * @author 
     **/
    public function getPageQuery($mid='')
    {
        $module = M('SysPagequery');
    }

    /**
     * 获取查询字段
     * @param  [type] $mid [description]
     * @return [type]      [description]
     */
    public function getQueryFields($mid)
    {
        $module = M('SysPagequery');
        $data = $module->field("fld_name,fld_type,$this->fld_lang as fld_text")->where("moduleid='$mid'")->select();
        $this->ajaxReturn($data, 'json');
    }
}